import java.util.Arrays;

class Solution {
    public static int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int max = nums.length - 1;
        int left = 0, right = max - 1;
        int result = 0;
        while (left < right) {
            while (left < right) {
                if (nums[left] + nums[right] > nums[max]) {
                    result += right - left;
                    right--;
                } else {
                    left++;
                }
            }
            max--;
            left = 0;
            right = max - 1;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] a = {1, 1, 3, 4};
        triangleNumber(a);
    }
}